package ${package}.controller.${classname};

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.imydao.safetystandard.basic.excel.ExcelStyleUtil;
import com.imydao.safetystandard.basic.excel.ExcelUtils;
import com.imydao.safetystandard.basic.excel.res.ExcelRes;
import com.imydao.safetystandard.basic.page.res.PageBean;
import com.imydao.safetystandard.basic.res.BaseRes;
import ${package}.config.interceptor.defined.CurrentUser;
import ${package}.controller.base.BaseController;
import ${package}.db.${classname}.model.${ClassName};
import ${package}.dto.${classname}.excel.${ClassName}ExcelExportDto;
import ${package}.dto.${classname}.excel.${ClassName}ExcelImportDto;
import ${package}.dto.${classname}.req.${ClassName}AddDto;
import ${package}.dto.${classname}.req.${ClassName}EditDto;
import ${package}.dto.${classname}.req.${ClassName}ImportDto;
import ${package}.dto.${classname}.req.${ClassName}ListDto;
import ${package}.dto.${classname}.res.${ClassName}DetailRes;
import ${package}.service.${classname}.${ClassName}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
* ${tableComment}
*
* @author ${author} ${email}
* @since ${version} ${date}
*/
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/${classname}")
@Api(tags="${tableComment}")
@AllArgsConstructor
public class ${ClassName}Controller extends BaseController {

    private final ${ClassName}Service ${className}Service;

    /**
    * 列表
    * @param dto 分页请求参数
    * @return 分页列表
    */
    @PostMapping("list")
    @ApiOperation("分页列表")
    public BaseRes<PageBean<${ClassName}>> list(@CurrentUser @RequestBody ${ClassName}ListDto dto){
        if (dto.getCustomerId() == null){
            dto.setCustomerId(getUser().getLogInCustomerId());
        }
        if (dto.getEnterpriseId() == null){
            dto.setEnterpriseId(getUser().getLogInEnterpriseId());
        }
        return ${className}Service.pageList(dto);
    }

    /**
    * 保存
    * @param dto 添加对象
    * @return 主键id
    */
    @PostMapping("save")
    @ApiOperation("保存")
    public BaseRes<Long> save(@CurrentUser @RequestBody @Valid ${ClassName}AddDto dto){
        if (dto.getCustomerId() == null){
            dto.setCustomerId(getUser().getLogInCustomerId());
        }
        dto.setLogInUserId(getUser().getLogInUserId());
        return ${className}Service.add(dto);
    }

    /**
    * 编辑
    * @param dto 编辑对象
    * @return 主键id
    */
    @PostMapping("edit")
    @ApiOperation("编辑")
    public BaseRes<Long> edit(@CurrentUser @RequestBody @Valid ${ClassName}EditDto dto){
        dto.setLogInUserId(getUser().getLogInUserId());
        return ${className}Service.edit(dto);
    }

    /**
    * excel导入
    * @param excel excel
    * @return excel导入条数
    */
    @PostMapping("importExcel")
    @ApiOperation("excel导入")
    public BaseRes<ExcelRes<${ClassName}ExcelImportDto>> importExcel(@CurrentUser ${ClassName}ImportDto dto,
                                        @RequestParam("excel") MultipartFile excel){
        if (excel == null || excel.getOriginalFilename() == null) {
            return BaseRes.getI18nError("basic.excel.file.not.exist");
        }
        if (!excel.getOriginalFilename().endsWith(".xls") && !excel.getOriginalFilename().endsWith(".xlsx")) {
            return BaseRes.getI18nError("basic.excel.please.upload.xls(x).excel");
        }
        if (dto.getCustomerId() == null){
            dto.setCustomerId(getUser().getLogInCustomerId());
        }
        dto.setLogInUserId(getUser().getLogInUserId());
        return ${className}Service.importExcel(excel, dto);
    }

    /**
    * 导出
    * @param dto 分页请求参数
    */
    @SneakyThrows(IOException.class)
    @PostMapping("exportExcel")
    @ApiOperation("分页列表")
    public void exportExcel(@CurrentUser @RequestBody ${ClassName}ListDto dto, HttpServletResponse response){
        if (dto.getCustomerId() == null){
            dto.setCustomerId(getUser().getLogInCustomerId());
        }
        if (dto.getEnterpriseId() == null){
            dto.setEnterpriseId(getUser().getLogInEnterpriseId());
        }
        List<${ClassName}ExcelExportDto> list = ${className}Service.exportExcel(dto);
        ExportParams exportParams = new ExportParams("${tableComment}", "${tableComment}数据");
        exportParams.setStyle(ExcelStyleUtil.class);
        ExcelUtils.exportExcel(list, ${ClassName}ExcelExportDto.class, "${tableComment}数据表", exportParams, response);
    }


    /**
    * 根据Id查询详情
    *
    * @param id 主键id
    * @return 详情
    */
    @GetMapping("detail/{id}")
    @ApiOperation(value = "详情")
    public BaseRes<${ClassName}DetailRes> detail(@PathVariable("id") Long id) {
        return ${className}Service.detail(id);
    }

    /**
    * 删除
    * @param id 主键id
    * @return 已删除id
    */
    @GetMapping("delete/{id}")
    @ApiOperation("删除")
    public BaseRes<Long> delete(@PathVariable("id") Long id){
        return ${className}Service.delete(id);
    }
}